<?php

namespace Home\Controller;

use \Home\Controller\IndexController;

class ClientAccountController extends IndexController {

    public function add() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        $this->assign('id', $id);
        if (IS_POST) {
            $model = D('Home/ClientAccount');
            if ($model->create(I('post.'), 1)) {
                if ($id = $model->add()) {
                    $this->success('添加成功！', U('lst?id=' . $id . '&p=' . $p));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $this->display();
    }

    public function edit() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        if (IS_POST) {
            $gid = I('post.client_id');
            $model = D('Home/ClientAccount');
            if ($model->create(I('post.'), 2)) {
                if ($model->save() !== FALSE) {
                    $this->success('修改成功！', U('lst', array('id' => $gid, 'p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $model = M('ClientAccount');
        $data = $model->find($id);
        $this->assign('data', $data);

        $this->display();
    }

    public function delete() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $model = D('Home/ClientAccount');
        if ($model->delete(I('get.id', 0)) !== FALSE) {
            $this->success('删除成功！', U('lst', array('id' => I('get.gid'), 'p' => $p)));
            exit;
        } else {
            $this->error($model->getError());
        }
    }

    public function lst() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);

        $id = I('get.id');
        $this->assign('id', $id);
        $model = D('Home/ClientAccount');
        $data = $model->search();
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        //取出客户名字
        $cModel = M('Client');
        $name = $cModel->field('name')->where(array('id' => array('eq', $id)))->find();
        $this->assign($name);

        $this->display();
    }

}
